module WordPuzzleHelper
  
  def replace_enigma_keys(text, enigma, user_match)
    result = text.gsub("{enigma.enigma}", enigma.enigma.to_s).gsub("{match.points}", user_match.points.to_s).gsub("{match.time}", seconds_to_time(user_match.seconds))
    result
  end
  
  def replace_user_keys(text, user)
    result = text.gsub("{users.points}", user.points.to_s).gsub("{user.best_time}", seconds_to_time(user.best_time)).gsub("{user.karma}", Karma.karma_for(user.points).name)
  end
  
  def build_js_wall_post(enigma, user, match)
    
    caption = replace_enigma_keys(GameConfig.game_config.publish_att_caption, enigma, match )
    caption = replace_user_keys(caption, user)
    
    description = replace_enigma_keys(GameConfig.game_config.publish_att_description, enigma, match )
    description = replace_user_keys(description, user)
    
    message = replace_enigma_keys(GameConfig.game_config.publish_message, enigma, match )
    message = replace_user_keys(message, user)
    
    
    
    js = "var publish = { method: 'stream.publish',display: 'dialog',"
    js += "message: '#{message}',"
    js += "attachment: { name: '#{GameConfig.game_config.publish_att_name}',caption: '#{caption}',"
    js += "description: ('#{description}'),"
    js += "href: '#{facebook_app_url}',"
    js += "media: [{type: 'image', src: '#{image_url(GameConfig.game_config.game_image.url)}', href: '#{facebook_app_url}'}]},"
    js += "action_links: [{"
    js += "text: '#{GameConfig.game_config.publish_action_link_text}',"
    js += "href: '#{facebook_app_url}'"
    js += "}],"
    js += "user_message_prompt: '#{GameConfig.game_config.publish_user_message_prompt}'"
    js += "};"
    
    js += "FB.ui(publish);"
  end
  
  
end
